import fetch from "../../utils/fetch";
const globalData = getApp().globalData;
const userId = globalData.userInfo.id;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordDate: '', // 记录的日期
    topBarData:[
      {
        id:0,
        name:'常用'
      },
      {
        id:1,
        name:'收藏'
      },
    ],
    navItem:0, //导航栏选中id
    show: false,
    selectArr: [
      {
        
      }
    ],
    oftenUseArr: [],
    collectArr: [],
    selectedSport : {},
    currentNumber: 0,
    unitHeat: 0,
    totalHeat: 0,
    recordSport: [],
    recordShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({recordDate: options.recordDate});
    this.getData();
  },
  getData(){
    fetch(`/sports/oftenAndCollect?userId=${userId}`,).then(res=>{
      this.setData({
        oftenUseArr: res.data.oftenUseArr,
        collectArr: res.data.collectArr,
        selectArr: this.data.navItem == 0 ? res.data.oftenUseArr : res.data.collectArr 
      })
    }).catch(err=>{
      this.onLoad(options);
    })
  },
   //子组件传过来的选中id
   changeItem:function(e){
    const index = e.detail.index
    if(index == 1){
      this.getData();
    }
    this.setData({
      navItem:index,
      selectArr: index == 0?this.data.oftenUseArr:this.data.collectArr
    });
  },
  showPopup(e){
    const index = e.currentTarget.dataset.index;
    const sport = this.data.selectArr[index].sports;
    this.setData({
      selectedSport:sport,
      unitHeat: sport.heat,
      totalHeat: 0,
      currentNumber: 0,
      show:true
    })
  },
  onClose(){
    this.setData({
      show: false,
    })
  },
  getCurrentNumber(e){
    let result = e.detail.current;
    const unitHeat  = this.data.unitHeat;
    const totalHeat = parseFloat(((unitHeat / 30) * result).toFixed(2));
    this.setData({
      totalHeat,
      currentNumber: result
    })
  },
  addSport(){
    const selectedSport = this.data.selectedSport;
    selectedSport.userId = userId;
    selectedSport.sportsId = selectedSport.id;
    selectedSport.totalHeat = this.data.totalHeat;
    selectedSport.num = this.data.currentNumber;
    selectedSport.recordDate = this.data.recordDate;;
    const recordSport = this.data.recordSport;
    recordSport.push(selectedSport);
    this.setData({
      recordSport,
      show: false
    })
  },
  showRecord(){
    this.setData({
      recordShow: true
    })
  },
  closeRecord(){
    this.setData({
      recordShow:false
    })
  },
  deleteSport(e){
    const i = e.currentTarget.dataset.index;
    const recordSport = this.data.recordSport;
    const newRecord = recordSport.filter((item, index) => index !== i);
    this.setData({
      recordSport: newRecord
    });
  },
  changeCollect(e){
    const sportId = e.currentTarget.dataset.sportId;
    const isCollect = this.data.selectedSport.isCollect;
    const selectArr = this.data.selectArr;
    const idx = selectArr.findIndex(item => item.sports.id === sportId);
    if(!isCollect){
      fetch(`/collect?userId=${userId}&id=${sportId}&collectType=sports`).then(res=>{
        if (res.code === 200) {
          console.log(selectArr[idx]);
          selectArr[idx].sports.isCollect = true;
          this.setData({
            'selectedSport.isCollect': !isCollect,
            selectArr
          });
        }
      })
    }else{
      fetch(`/collect/uncollect?userId=${userId}&id=${sportId}&collectType=sports`).then(res=>{
        if (res.code === 200) {
          selectArr[idx].sports.isCollect = false;
          this.setData({
            'selectedSport.isCollect': !isCollect,
            selectArr
          });
        }
      })
    }
  },
  addRecord(){
    fetch('/sportsrecord/add',{
      sportsRecord: this.data.recordSport
    },'post').then(res=>{
      if(res.code === 200){
        wx.showToast({
          title: '打卡成功',
          icon: 'success'
        })
        wx.navigateBack();
      }
    }).catch(err=>{
      this.addRecord();
    })
  },
  gotoSearch(){
    wx.navigateTo({
      url: `../search/search?fromId=2&recordDate=${this.data.recordDate}`,
    })
  }
})